package com.ls.lserp.modules.setting.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ls.lserp.common.persistence.Page;
import com.ls.lserp.common.service.CrudService;
import com.ls.lserp.modules.setting.dao.CompanyUserDao;
import com.ls.lserp.modules.setting.entity.CompanyUser;
import com.ls.lserp.modules.sys.utils.SequenceUtil;

@Service
@Transactional(readOnly = true)
public class CompanyUserService extends CrudService<CompanyUserDao, CompanyUser> {

	@Autowired
	private CompanyUserDao companyUserDao;

	/**
	 * @Title: list @Description: 一览 @param @param page @param @param
	 * companyUser @param @return @return Page<CompanyUser> @throws
	 */

	public CompanyUser getDutyList(CompanyUser companyUser) {
		companyUser.setUserList(companyUserDao.getDutyList(companyUser));
		return companyUser;
	}

	public CompanyUser getUserGroupList(CompanyUser companyUser) {
		companyUser.setGroupList(companyUserDao.getUserGroupList(companyUser));
		return companyUser;
	}

	public CompanyUser getRoleList(CompanyUser companyUser) {
		companyUser.setRoleList(companyUserDao.getRoleList(companyUser));
		return companyUser;
	}

	public CompanyUser getOgnizationList(CompanyUser companyUser) {
		companyUser.setOgnizationList(companyUserDao.getOgnizationList(companyUser));
		return companyUser;
	}

	/**
	 * @Title: findList @Description: 一览 @param @param page @param @param
	 * companyUser @param @return @return Page<CompanyUser> @throws
	 */

	public Page<CompanyUser> findList(Page<CompanyUser> page, CompanyUser companyUser) {
		// 设置分页参数
		companyUser.setPage(page);
		// 执行分页查询
		page.setList(companyUserDao.findList(companyUser));

		return page;
	}

	/**
	 * @Title: saveCompanyUser @Description: 保存数据 @param @param dict @return
	 * void @throws
	 */

	@Transactional(readOnly = false)
	public void saveCompanyUser(CompanyUser companyUser) {
		// 判断新建编辑
		// 若为null 执行插入语句（新建）
		if (companyUser.getId() == null) {
			companyUser.preInsert();
			// 大连振宇服装厂
			if (companyUser.getOgnizationId() == 1) {
				companyUser.setUserBarcode(SequenceUtil.commonCode("ZY-", "sequence_userno", 6).getReturnCode());
				// 大连碎片智能
			} else {
				companyUser.setUserBarcode(SequenceUtil.commonCode("Frag-", "sequence_userno_frag", 6).getReturnCode());
			}
			companyUserDao.insertLoginUser(companyUser);
			long tempId = companyUser.getId();
			companyUser.setId(tempId);
			companyUserDao.insertOgnizationUser(companyUser);
			companyUserDao.insertGroupUser(companyUser);
			companyUserDao.insertUserRole(companyUser);
			// 编辑
		} else {
			// 更新数据
			companyUser.preUpdate();
			companyUserDao.updateLoginUser(companyUser);
			companyUserDao.updateOgnizationUser(companyUser);
			companyUserDao.updateGroupUser(companyUser);
			companyUserDao.updateUserRole(companyUser);

		}
	}

	/**
	 * @Title: deleteCompanyUser @Description: 删除公司员工 @param @param
	 * companyUser @return void @throws
	 */

	@Transactional(readOnly = false)
	public void deleteCompanyUser(CompanyUser companyUser) {

		// 逻辑删除
		companyUserDao.delete(companyUser);

	}

}
